<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreActive;
use App\Http\Requests\StoreGood;
use App\Http\Requests\StoreMember;
use App\Http\Requests\UpdateActive;
use App\Http\Requests\UpdateGood;
use App\Http\Requests\UpdateMember;
use App\Models\Active;
use App\Models\ActiveOncustomer;
use App\Models\Coupon;
use App\Models\Good;
use App\Models\Memberplan;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_members' => 'am-in',
            '_member' => 'am-active',
        ]);

    }


    // 通知活动首页
    public function index()
    {
        $items = Memberplan::with('store')->orderBy('sort')->get();

        return view('admin.member.index',compact('items'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        $cates = Store::all();

        return view('admin.member.create', compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreMember $request)
    {

        $info = $request->all();
        Memberplan::create($info);
        return redirect('admin/member')->with('suceess', '新增成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cates = Store::all();

        $item = Memberplan::find($id);
        return view('admin.member.edit', compact('cates', 'item'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateMember$request, $id)
    {
        $item = Memberplan::find($id);

        $item->update($request->all());
        return redirect('admin/member')->with('success', '修改成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Good::destroy($id);
        return back()->with('success','删除成功');

    }

    //是否使用
    public function is_use(Request $request)
    {

        //当前年卡信息
        $item = Memberplan::find($request->id);

        if ($item->is_use == 0) {
            $data['is_use'] = 1;
            $item->update($data);
        } else {
            $data['is_use'] = 0;
            $item->update($data);
        }
    }

    //是否优惠
    public function is_active(Request $request)
    {
        //当前年卡信息
        $item = Memberplan::find($request->id);

        if ($item->is_active == 0) {
            $data['is_active'] = 1;
            $item->update($data);
        } else {
            $data['is_active'] = 0;
            $item->update($data);
        }
    }



    //Ajax排序
    public function sort(Request $request)
    {
        //当前年卡
        $coupon = Coupon::find($request->id);
        //更新sort字段
        $coupon->sort = $request->sort;
        $coupon->save();
    }





}
